package com.wtgroup.demo.牛客;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

/**
 * @author nisus
 * @version 0.1
 * @since 2023/6/6 15:49
 */
public class HJ89_24点运算 {

    // 用例
    // A 2 J 3
    // 4 2 K A

    // 剩余 思路
    // 先选数字，再选符号
    static class D {
        static HashMap<String, Integer> map = new HashMap<>();

        static {
            for (int i = 2; i <= 10; i++) {
                map.put(String.valueOf(i), i);
            }
            map.put("A", 1);
            map.put("J", 11);
            map.put("Q", 12);
            map.put("K", 13);
        }

        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            String line = null;
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                line = in.nextLine();
                break;
            }

            String[] ss = line.split(" ");
            int[] nums = new int[4];
            for (int i = 0; i < 4; i++) {
                String e = ss[i];
                if (e.equalsIgnoreCase("joker")) {
                    System.out.println("ERROR");
                    return;
                }
                Integer n = map.get(e);
                nums[i] = n;
            }

            String exp = func(nums, ss, 24, new HashSet<>());

            System.out.println(exp == null ? "NONE" : exp);
        }

        static String func(int[] nums, String[] ss, int target, HashSet<Integer> indexMark) {
            // int currVal = nums[curr];
            // if (curr == nums.length - 1) {
            //     return currVal == target ? ss[curr] : null;
            // }


            for (int i = 0; i < nums.length; i++) {
                int curr = i;
                int currVal = nums[i];
                if (indexMark.contains(curr)) continue;

                indexMark.add(curr);

                if (indexMark.size() == nums.length && currVal == target) {
                    return ss[i];
                }

                // rest + curr
                String rest = func(nums, ss, target - currVal, indexMark);
                if (rest != null) {
                    // 可行解之一
                    return rest + "+" + ss[curr];
                }

                // rest - curr
                rest = func(nums, ss, target + currVal, indexMark);
                if (rest != null) {
                    // 可行解之一
                    return rest + "-" + ss[curr];
                }

                // rest * curr
                if (target % currVal == 0) { // 整除才可以可能
                    rest = func(nums, ss, target / currVal, indexMark);
                    if (rest != null) {
                        // 可行解之一
                        return rest + "*" + ss[curr];
                    }
                }

                // rest / curr
                rest = func(nums, ss, target * currVal, indexMark);
                if (rest != null) {
                    // 可行解之一
                    return rest + "/" + ss[curr];
                }

                indexMark.remove(curr);
            }


            return null;
        }

    }

    // 累计思路，先选符号，再选数字，累计到已有结果上，判断是否可能
    static class D2 {
        static HashMap<String, Integer> map = new HashMap<>();

        static {
            for (int i = 2; i <= 10; i++) {
                map.put(String.valueOf(i), i);
            }
            map.put("A", 1);
            map.put("J", 11);
            map.put("Q", 12);
            map.put("K", 13);
        }

        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            String line = null;
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                line = in.nextLine();
                break;
            }

            String[] ss = line.split(" ");
            int[] nums = new int[4];
            for (int i = 0; i < 4; i++) {
                String e = ss[i];
                if (e.equalsIgnoreCase("joker")) {
                    System.out.println("ERROR");
                    return;
                }
                Integer n = map.get(e);
                nums[i] = n;
            }

            String exp = main(nums, ss);

            System.out.println(exp == null ? "NONE" : exp);
        }

        static String main(int[] nums, String[] ss) {
            // 第一个数字前面，只能是 +
            // 每个数字作为第一个，都尝试一遍
            for (int i = 0; i < nums.length; i++) {
                int currVal = nums[i];
                nums[i] = -1; // 标记已经选择过了，或用一个set标记
                String r = func(nums, ss, 24, currVal, 1, "" + ss[i]);
                nums[i] = currVal; // 恢复现场
                if (r != null) {
                    return r;
                }
            }

            return null;
        }

        static String func(int[] nums, String[] ss, int target, int res, int slotIx, String exp) {
            if (slotIx >= nums.length) {
                return res == target ? exp : null;
            }

            String expRes = null;
            for (int i = 0; i < nums.length; i++) {
                int currVal = nums[i];
                if (currVal == -1) continue; //不可再重复选
                nums[i] = -1;

                // res + currVal
                expRes = func(nums, ss, target, res + currVal, slotIx + 1, exp + "+" + ss[i]);
                if (expRes != null) return expRes;

                expRes = func(nums, ss, target, res - currVal, slotIx + 1, exp + "-" + ss[i]);
                if (expRes != null) return expRes;

                expRes = func(nums, ss, target, res * currVal, slotIx + 1, exp + "*" + ss[i]);
                if (expRes != null) return expRes;

                if (res % currVal == 0) {
                    expRes = func(nums, ss, target, res / currVal, slotIx + 1, exp + "/" + ss[i]);
                    if (expRes != null) return expRes;
                }

                nums[i] = currVal;
            }

            return expRes;
        }

    }
}
